package neu.soft.service.impl;

import neu.soft.entity.Inventory;
import neu.soft.mapper.InventoryMapper;
import neu.soft.service.InventoryService;
import neu.soft.vo.PageBean;
import neu.soft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class InventoryServiceImpl implements InventoryService {
    @Autowired
    private InventoryMapper inventoryMapper;

    /**
     * 根据商品代码查询库存商品数据
     * @author zhh
     * @param map
     * @return
     */
    @Override
    public ResultBean<PageBean<Map<String,Object>>> queryInventoryInfo(Map<String,Object> map){
        ResultBean<PageBean<List<Map<String,Object>>>> resultBean = new ResultBean<>();
        PageBean<Map<String,Object>> pageBean = new PageBean<>();
        int pageNow = (int) map.get("pageNow");
        int pageSize = (int) map.get("pageSize");
        List<Map<String,Object>> inventoryList = inventoryMapper.queryInventoryInfo(map);
        Long inventoryRows = inventoryMapper.getInventoryRows(map);
        if(inventoryList == null){
            return ResultBean.error(404,"找不到库存对象");
        }
        else {
            pageBean.setRowData(inventoryList);
            pageBean.setTotalRows(inventoryRows);
            pageBean.setTotalPages((int) (inventoryRows % pageSize == 0 ? (inventoryRows/pageSize) : (inventoryRows/pageSize + 1)));
            return ResultBean.success(pageBean);
        }
    }

    /**
     * 查询所有库存商品,此时map中只有pageNow和pageSize
     * @author zhh
     * @return
     */
    @Override
    public ResultBean<PageBean<Map<String,Object>>> showAllInventory(Map<String,Object> map){
        ResultBean<PageBean<List<Map<String,Object>>>> resultBean = new ResultBean<>();
        PageBean<Map<String,Object>> pageBean = new PageBean<>();
        int pageNow = (int) map.get("pageNow");
        int pageSize = (int) map.get("pageSize");
        List<Map<String,Object>> inventoryList = new ArrayList<>();
        inventoryList = inventoryMapper.showAllInventory(map);
        Long inventoryRows = inventoryMapper.getInventoryRows(map);
        if(inventoryList == null){
            return ResultBean.error(404,"找不到库存对象");
        }
        else {
            pageBean.setRowData(inventoryList);
            pageBean.setTotalRows(inventoryRows);
            pageBean.setTotalPages((int) (inventoryRows % pageSize == 0 ? (inventoryRows/pageSize) : (inventoryRows/pageSize + 1)));
            return ResultBean.success(pageBean);
        }
    }

    /**
     * 添加库存商品
     * @author zhh
     * @param map
     * @return
     */
    @Override
    public ResultBean<Integer> addInventory(Map<String,Object> map){
        String code = (String) map.get("code");
        String inventoryName = (String)map.get("inventoryName");
        int num = (Integer)map.get("num");
        int warehouseId = (Integer)map.get("warehouseId");

        Inventory inv = new Inventory(code,inventoryName,num,warehouseId);
        int result = inventoryMapper.addInventory(inv);
        return ResultBean.success();
    }

    /**
     * 根据商品代码和所属库房ID删除商品信息
     * @author zhh
     * @param map
     * @return
     */
    @Override
    public ResultBean<Integer> deleteInventory(Map<String,Object> map){
        int result = inventoryMapper.deleteInventory(map);
        return ResultBean.success();
    }

    /**
     * 改变指定库房的特定商品的数量
     * @author zhh
     * @param map
     * @return
     */
    @Override
    public ResultBean<Integer> increaseInventoryNum(Map<String,Object> map){
        int result = inventoryMapper.increaseInventoryNum(map);
        return ResultBean.success();
    }

    /**
     * 减少指定库房内的特定商品的数量
     * @author zhh
     * @param map
     */
    @Override
    public void decreaseInventoryNum(Map<String,Object> map){
        inventoryMapper.decreaseInventoryNum(map);
    }

    /**
     * 根据库房编号和商品代码查询商品数量
     * @param map
     * @return
     */
    @Override
    public ResultBean<Integer> queryInventoryNum(Map<String,Object> map){
        int num = inventoryMapper.queryInventoryNum(map);

        if(num == 0){
            return ResultBean.error();
        }
        else {
            return ResultBean.success(num);
        }
    }

    /**
     * 根据商品代码查询中心库房商品
     * @param codeList
     * @return
     */
    @Override
    public List<Inventory> queryInventoryByCodeList(List<String> codeList){
        List<Inventory> iList = new ArrayList<>();
        for(int i = 0;i < codeList.size();i++){
            Inventory inv = inventoryMapper.queryInventoryByCodeList(codeList.get(i));
            iList.add(inv);
        }
        return iList;
    }

    public int existInventory(Map<String,Object> map){
        return inventoryMapper.existInventory(map);
    }

    public ResultBean<List<Map<String,Object>>> queryInventoryByWarehouseId(int warehouseId){
        List<Map<String,Object>> iList = inventoryMapper.queryInventoryByWarehouseId(warehouseId);
        return ResultBean.success(iList);
    }
}
